package com.dean.practice.producer.ctrl;


import io.swagger.annotations.ApiOperation;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;


/**
 * @author Dean
 * @date 2020-11-27
 */
@RestController
public class UaaCtrl {

    @ApiOperation("获取登录用户")
    @GetMapping(path = "/")
    public Object userInfo(@AuthenticationPrincipal Principal principal) {
        if (principal instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) principal;
            return user.getPrincipal();
        } else if (principal instanceof OAuth2AuthenticationToken) {
            OAuth2AuthenticationToken user = (OAuth2AuthenticationToken) principal;
            return user.getPrincipal();
        }
        return principal;
    }
}